package com.working.user.widget

import android.graphics.*
import android.text.TextPaint
import android.view.View
import androidx.core.graphics.toColorInt
import androidx.core.view.marginRight
import androidx.recyclerview.widget.RecyclerView
import com.working.libcommon.exts.cast
import com.working.libcommon.util.LogUtils
import com.working.user.UserActivityMainActivity
import com.working.user.model.DateBean
import dp

class MItemDecoration : RecyclerView.ItemDecoration() {
    val textPaint by lazy {
        TextPaint().apply {
            bgColor = Color.RED
            textSize = 20f.dp
            color = "#FF254FA1".toColorInt()
        }
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        val childCount = parent.childCount
        val adapter = parent.adapter?.cast<UserActivityMainActivity.DataAdapter>() ?: return
        val currentList = adapter.currentList
        for (i in 0 until childCount) {
            val view = parent.getChildAt(i)
            val currentData = currentList[i]
            val previousData = if (i > 0) currentList[i - 1] else null
            currentData.event.forEach {
                var offset = (-21).dp * (currentData.event.indexOf(it))
                var index = i + 1
                var next: DateBean?
                var width = view.width + view.marginRight.toFloat()
                while (kotlin.run {
                        next =
                            if (index < currentList.size) currentList[index] else null
                        next
                    }?.event?.contains(it) == true) {
                    index++
                    next =
                        if (index < currentList.size) currentList[index] else null
                    width += (view.width + view.marginRight)
                }
                val maxSize =
                    textPaint.breakText(it.toCharArray(), 0, it.length, width, FloatArray(1))
                offset += 21.dp
                val height = textPaint.fontMetrics.bottom - textPaint.fontMetrics.top
                if (previousData?.event?.contains(it) != true) {
                    val rect = RectF(
                        view.left.toFloat(),
                        (view.bottom - view.top) / 2f + view.top + offset - height,
                        view.left.toFloat() + width - view.marginRight / 2,
                        (view.bottom - view.top) / 2f + view.top + offset
                    )
                    c.drawRect(
                        rect,
                        Paint().apply {
                            color = Color.RED
                        }
                    )
                    c.save()
                    c.clipRect(rect)
                    c.drawText(
                        it,
                        0,
                        it.length,
                        view.left.toFloat(),
                        (view.bottom - view.top) / 2f + view.top + offset,
                        textPaint
                    )
                    c.restore()
                    LogUtils.e(width)

                }
            }
        }
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
    }


    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {

        outRect.set(view.left, view.top, view.right, view.bottom)
    }
}